﻿using System;
using System.Collections.Generic;

using NUnit.Framework;

using VidoopSecure;
using VidoopSecureExtensionMethods;

namespace TestVidoopSecure
{
    [TestFixture]
    public class TestCaptchaService : BaseTest
    {
        [TestFixtureSetUp]
        public override void SetUp()
        {
            base.SetUp();
        }

        [TestFixtureTearDown]
        public override void TearDown()
        {
            base.TearDown();
        }

        [Test]
        public void Test_GetService()
        {
            Service service = Manager.GetService(
                "captcha", CurrentSite);
            Assert.IsNotNull(service);
        }

        [Test]
        public void Test_NewCaptcha()
        {
            CaptchaService service = (CaptchaService)Manager.GetService(
                "captcha", CurrentSite);
            Assert.IsNotNull(service);

            string[] categories = { "cars0001", "people01", "lksnkys1", "wldanml1",
            "castles1", "houses01", "trains01", "flowers1", "dogs0001", "food0001",
            "skyscrp1", "stndgls1", "muscins1", "tysngms1", "cats0001", "bevrags1",
            "insects1", "comptrs1", "outspac1", "birds001", "boats001", "airplns1",
            "undrwtr1", "money001", "telphns1", "clocks01" };

            string bundle = string.Join(":", categories.Slice(0, 12));
            
            int captchaLength = 3;
            bool orderMatters = true;
            int width = 4;
            int height = 3;
            Captcha captcha = service.NewCaptcha(captchaLength, orderMatters, width, height);

            Assert.IsNotNull(captcha.Id);
            Assert.AreEqual(captchaLength, captcha.Length);
            Assert.AreEqual(orderMatters, captcha.OrderMatters);
            Assert.AreEqual(width, captcha.Width);
            Assert.AreEqual(height, captcha.Height);
            Assert.AreEqual(Settings.ApiBase + "/captchas/" + captcha.Id + "/image", captcha.Image);
            Assert.IsFalse(captcha.Attempted);
            Assert.IsFalse(captcha.Authenticated);

            // attempt code verification
            bool verifyresult = service.VerifyCode(captcha.Id, "a");
            Assert.IsFalse(verifyresult);

            // see that verify attempt has been recorded
            Captcha captcha2 = service.GetCaptcha(captcha.Id);
            Assert.AreEqual(captcha.Id, captcha2.Id);
            Assert.IsTrue(captcha2.Attempted);
        }
    }
}
